import os
import re
from tqdm import tqdm
import json
from basic_component_related_management.models import *
from control.models.User import User
from unit.models import *
from literature.models import *
from dataset.models import *
from django.db import IntegrityError, transaction
import traceback

# 创建一个superadmin，密码是superadmin1234
user_entity = User.objects.filter(user_account="superadmin").first()
if user_entity is None:
    user_entity = User.objects.create(
        user_account="superadmin",
        user_name="superadmin",
        password="C6IDQGUT42vVUX0neDCF2TB2kD42tBWwp4AGqp3DenmkTYI9urFbhJomuOMcI25De98AjQJPBT07AUW5oQdYeHfeQKr/uhR6OABnKu+/uof5viBguKPdDqkc/znPMjADdt66dxVi/yeur8EahDaUCvhssdB4HkJLxteCLGlEDZ/AJe1BiTBCgyN+7ZLCEjxXD4lKbI5mT9oWEQ+Pd1rE7fY/jmuDh6Nnz2YO7/0WvQA39KESBVzrVQ5bmEVSnUNkQqfzR8DkylHQ54swmNGXFog9xd7q0gjd2VrsBfE6qD4y+iJThQlALLh1f9QdVthAcuepLUKW+PI/mmyllQTufw=="
    )

# 创建单位的基本内容
dimension_less_entity = Dimension.objects.filter(
    dimension_id="D_Dimensionless").first()
if dimension_less_entity is None:
    dimension_less_entity = Dimension.objects.create(
        dimension_name="无量纲",
        dimension_name_en="dimensionless",
        dimension_id="D_Dimensionless",
        basic_unit="1",
        description="无量纲",
        create_person=user_entity,
        modify_person=user_entity,
    )
unit_entity = Unit.objects.filter(symbol="1").first()
if unit_entity is None:
    unit_entity = Unit.objects.create(
        unit_name="无量纲",
        unit_name_en="dimensionless",
        symbol="1",
        dimension=dimension_less_entity,
        is_system = True,
        create_person=user_entity,
        modify_person=user_entity,
    )
    UnitSymbol.objects.create(
        symbol="1",
        root_unit=unit_entity,
        is_system = True,
        create_person=user_entity,
        modify_person=user_entity,
    )

# 创建量
quantity_entity = Quantity.objects.filter(quantity_name="ddb量").first()
if quantity_entity is None:
    quantity_entity = Quantity.objects.create(
        quantity_name="ddb量",
        quantity_name_en="ddb_quantity",
        quantity_type="NORMAL",
        dimension=dimension_less_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    QuantityUnitMap.objects.create(
        quantity=quantity_entity,
        unit=unit_entity)

# 文献部分的单位
quantity_purity_entity = Quantity.objects.filter(quantity_name="样品纯度").first()
if quantity_purity_entity is None:
    quantity_purity_entity = Quantity.objects.create(
        quantity_name="样品纯度",
        quantity_name_en="sample_purity",
        quantity_type="NORMAL",
        dimension=dimension_less_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    QuantityUnitMap.objects.create(
        quantity=quantity_purity_entity,
        unit=unit_entity)
percent_unit_entity = Unit.objects.filter(symbol="%").first()
if percent_unit_entity is None:
    percent_unit_entity = Unit.objects.create(
        unit_name="百分比",
        unit_name_en="percent",
        symbol="%",
        is_system = True,
        dimension=dimension_less_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    UnitConversion.objects.create(
        base_unit=unit_entity,
        target_unit=percent_unit_entity,
        factor=100,
        offset=0,
        create_person=user_entity,
        modify_person=user_entity,
    )
    UnitSymbol.objects.create(
        symbol="%",
        is_system = True,
        root_unit=percent_unit_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    QuantityUnitMap.objects.create(
        quantity=quantity_purity_entity,
        unit=percent_unit_entity)

# 初始化一些ddb的属性
DDB_ID_entity = Attribute.objects.filter(
    main_dataIndex="system_ddb_id"
).first()
if DDB_ID_entity is None:
    DDB_ID_entity = Attribute.objects.create(
        main_name="DDB编号",
        main_name_en="ddb_id",
        main_dataIndex="system_ddb_id",
        main_datatype="short_string",
        main_necessary=False,
        common_used=True,
        comment="系统初始化,DDB ID",
        is_system_config=True,
        create_person=user_entity,
        modify_person=user_entity,
    )
DSI_ID_entity = Attribute.objects.filter(
    main_dataIndex="system_dsi_id"
).first()
if DSI_ID_entity is None:
    DSI_ID_entity = Attribute.objects.create(
        main_name="DSI编号",
        main_name_en="dsi_id",
        main_dataIndex="system_dsi_id",
        main_datatype="short_string",
        main_necessary=False,
        common_used=True,
        comment="系统初始化,DSI ID",
        is_system_config=True,
        create_person=user_entity,
        modify_person=user_entity,
    )
Other_name_entity = Attribute.objects.filter(
    main_dataIndex="system_other_name").first()
if Other_name_entity is None:
    Other_name_entity = Attribute.objects.create(
        main_name="其他名称",
        main_name_en="Other Name",
        main_dataIndex="system_other_name",
        main_datatype="short_string",
        main_necessary=False,
        common_used=True,
        comment="系统初始化,其他名称",
        multiple_value_used=True,
        is_system_config=True,
        create_person=user_entity,
        modify_person=user_entity,
    )

# 文献部分的一些内容
status_entity = LiteratureStatusType.objects.filter(
    name="Available").first()
if status_entity is None:
    status_entity = LiteratureStatusType.objects.create(
        name="Available",
        zh_name="可用",
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )

liter_type_entity = LiteratureType.objects.filter(
    name="Unspecified").first()
if liter_type_entity is None:
    liter_type_entity = LiteratureType.objects.create(
        name="Unspecified",
        zh_name="未指定",
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )

language_entity = Language.objects.filter(
    name="unknown").first()
if language_entity is None:
    language_entity = Language.objects.create(
        name="unknown",
        zh_name="未知",
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )

DataPresentWay_entity = DataPresentWay.objects.filter(
    name="Unknown").first()
if DataPresentWay_entity is None:
    DataPresentWay_entity = DataPresentWay.objects.create(
        name="Unknown",
        zh_name="未知",
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )

purpose_entity = ExpPurpose.objects.filter(
    name="unknown").first()
if purpose_entity is None:
    purpose_entity = ExpPurpose.objects.create(
        name="unknown",
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )

# ddb物性组
property_group_entity = PropertyGroup.objects.filter(
    name="ddb物性类别").first()
if property_group_entity is None:
    property_group_entity = PropertyGroup.objects.create(
        name="ddb物性类别",
        name_en="ddb",
        description="ddb数据初始化",
        is_pure_used=True,
        is_binary_used=True,
        is_ternary_used=True,
        is_reaction_used=True,
        is_complex_used=True,

        create_person=user_entity,
        modify_person=user_entity,
    )


def is_decimal_or_scientific(s):
    # 正则表达式匹配正负小数或科学记数法
    scientific_pattern = re.compile(r'^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$')
    return bool(scientific_pattern.match(s))


def store_substance(substance_list_json):
    substance_DSI_substance_dict = {}
    for substance_content in substance_list_json:
        # 判断这个组分是否已经存在
        DDB_ID = substance_content["ExtId_DDB"]
        DSI_ID = substance_content["DSI"]
        CAR = substance_content["CAR"] if "CAR" in substance_content else ""
        if ComponentAttributeValue.objects.filter(
            attribute=DSI_ID_entity,
            main_value=json.dumps(DSI_ID)
        ).first() is not None:
            substance_DSI_substance_dict[DSI_ID] = ComponentAttributeValue.objects.filter(
                attribute=DSI_ID_entity,
                main_value=json.dumps(DSI_ID)
            ).first().componentidentifier
            continue
        # 否则需要创建
        Formula = substance_content["Formula"] if "Formula" in substance_content else ""
        InChiKey = substance_content["InChiKey"] if "InChiKey" in substance_content else ""
        Smiles = substance_content["Smiles"] if "Smiles" in substance_content else ""
        SubstanceClass_D = substance_content["SubstanceClass_D"]
        SubstanceClass_E = substance_content["SubstanceClass_E"]
        Synonym = substance_content["Synonym"]
        Synonym_D = substance_content["Synonym_D"]
        Synonym_E = substance_content["Synonym_E"]
        SystematicName_D = substance_content["SystematicName_D"] if "SystematicName_D" in substance_content else ""
        SystematicName_E = substance_content["SystematicName_E"] if "SystematicName_E" in substance_content else ""

        component_item = {}
        component_item["name"] = SystematicName_D
        component_item["name_en"] = SystematicName_E
        component_item["molecular_formula"] = Formula
        component_item["cas_number"] = CAR
        component_item["smiles_number"] = Smiles
        component_item["inchi_number"] = InChiKey
        component_item["description"] = "ddb数据初始化"
        component_item["create_person"] = user_entity
        component_item["modify_person"] = user_entity
        component_entity1 = ComponentIdentifier.objects.create(
            **component_item)
        substance_DSI_substance_dict[DSI_ID] = component_entity1

        # 处理DSI
        component_dsi_entity = ComponentAttributeValue.objects.create(
            componentidentifier=component_entity1, attribute=DSI_ID_entity, main_value=json.dumps(DSI_ID))

        # 处理DDB ID
        component_ddb_entity = ComponentAttributeValue.objects.create(
            componentidentifier=component_entity1, attribute=DDB_ID_entity, main_value=json.dumps(DDB_ID))

        # 处理别名
        all_alias = []
        all_alias.extend(Synonym)
        all_alias.extend(Synonym_D)
        all_alias.extend(Synonym_E)
        component_alias_entity = ComponentAttributeValue.objects.create(
            componentidentifier=component_entity1, attribute=Other_name_entity, main_value=json.dumps(all_alias))

        # 处理组分类别
        for i in range(len(SubstanceClass_D)):
            component_group_name = SubstanceClass_D[i]
            component_group_name_en = SubstanceClass_E[i]
            component_group_entity = ComponentGroup.objects.filter(
                name=component_group_name).first()
            if component_group_entity is None:
                component_group_entity = ComponentGroup.objects.create(
                    name=component_group_name,
                    name_en=component_group_name_en,
                    description="ddb数据初始化",
                    create_person=user_entity,
                    modify_person=user_entity,
                )
            ComponentIdeComponentGroupMap.objects.create(
                componentidentifier=component_entity1,
                componentGroup=component_group_entity,
            )
    return substance_DSI_substance_dict


def store_literature(literature_list_json):
    literature_ID_literature_dict = {}
    for literature_content in literature_list_json:
        DDB_ID = literature_content["ID"]
        # 查找有无
        if Literature.objects.filter(cit_id=DDB_ID, cit_name="DDB").first() is not None:
            literature_ID_literature_dict[DDB_ID] = Literature.objects.filter(
                cit_id=DDB_ID).first()
            continue
        literature_item = {}
        other_json = {}
        comment_content = ""
        # 创建作者
        author1 = None
        if "Authors" in literature_content:
            authors = literature_content["Authors"]
            author1 = Author.objects.filter(name=authors).first()
            if author1 is None:
                author1 = Author.objects.create(
                    name=authors,
                    creator=user_entity,
                    last_editor=user_entity,
                    reviewer=user_entity,
                )
        if "Title_E" in literature_content:
            literature_item["title"] = literature_content["Title_E"]
        else:
            literature_item["title"] = DDB_ID
        if "Title" in literature_content:
            literature_item["zh_title"] = literature_content["Title"]
        else:
            literature_item["zh_title"] = DDB_ID
        if "Title" in literature_content:
            other_json["Title"] = literature_content["Title"]
        if "Title_D" in literature_content:
            other_json["Title_D"] = literature_content["Title_D"]
        if "ID" in literature_content:
            other_json["DDB_ID"] = literature_content["ID"]
            comment_content += "DDB_ID: " + literature_content["ID"] + "\n"
        if "Journal" in literature_content:
            other_json["Journal"] = literature_content["Journal"]
            comment_content += "Journal: " + \
                literature_content["Journal"] + "\n"
        if "Coden" in literature_content:
            other_json["Coden"] = literature_content["Coden"]
            comment_content += "Coden: " + literature_content["Coden"] + "\n"
        if "Volume" in literature_content:
            literature_item["volume"] = literature_content["Volume"]
        if "Pages" in literature_content:
            page_list = literature_content["Pages"].split("-")
            if len(page_list) == 1:
                if page_list[0].isdigit():
                    literature_item["first_page"] = int(page_list[0])
                    literature_item["last_page"] = int(page_list[0])
            elif len(page_list) == 2:
                if page_list[0].isdigit():
                    literature_item["first_page"] = int(page_list[0])
                if page_list[1].isdigit():
                    literature_item["last_page"] = int(page_list[1])
            other_json["DDB_Pages"] = literature_content["Pages"]
        if "Year" in literature_content:
            other_json["DDB_Year"] = literature_content["Year"]
            year_list = literature_content["Year"].split("-")
            if len(year_list) == 1:
                if year_list[0].isdigit():
                    literature_item["date"] = year_list[0]+"-01-01"
            elif len(year_list) == 2:
                year_string = ""
                if year_list[0].isdigit():
                    year_string += year_list[0]
                    if year_list[1].isdigit():
                        year_string += "-" + year_list[1]
                        literature_item["date"] = year_string + "-01"
                    else:
                        literature_item["date"] = year_string + "-01-01"
            elif len(year_list) == 3:
                year_string = ""
                if year_list[0].isdigit():
                    year_string += year_list[0]
                    if year_list[1].isdigit():
                        year_string += "-" + year_list[1]
                        if year_list[2].isdigit():
                            year_string += "-" + year_list[2]
                            literature_item["date"] = year_string
                        else:
                            literature_item["date"] = year_string + "-01"
                    else:
                        literature_item["date"] = year_string + "-01-01"
        if "ISBN" in literature_content:
            other_json["ISBN"] = literature_content["ISBN"]
            comment_content += "DDB_ISBN: " + literature_content["ISBN"] + "\n"
        if "Collection" in literature_content:
            other_json["Collection"] = literature_content["Collection"]
            comment_content += "Collection: " + \
                literature_content["Collection"] + "\n"
        if "DocType" in literature_content:
            other_json["DocType"] = literature_content["DocType"]
            comment_content += "DocType: " + \
                literature_content["DocType"] + "\n"
        if "BibLanguage" in literature_content:
            language_entity_1 = Language.objects.filter(
                name=literature_content["BibLanguage"]).first()
            if language_entity_1 is None:
                language_entity_1 = Language.objects.create(
                    name=literature_content["BibLanguage"],
                    zh_name=literature_content["BibLanguage"],
                    creator=user_entity,
                    last_editor=user_entity,
                    reviewer=user_entity,
                )
            literature_item["language"] = language_entity_1
        else:
            literature_item["language"] = language_entity
        if "DOI" in literature_content:
            literature_item["DOI"] = literature_content["DOI"]
        if "Issue" in literature_content:
            literature_item["issue"] = literature_content["Issue"]
        if "CorporateSource" in literature_content:
            other_json["DDB_CorporateSource"] = literature_content["CorporateSource"]
            comment_content += "DDB_CorporateSource: " + \
                literature_content["CorporateSource"] + "\n"
        if "Editor" in literature_content:
            other_json["DDB_Editor"] = literature_content["Editor"]
            comment_content += "DDB_Editor: " + \
                literature_content["Editor"] + "\n"
        if "Publisher" in literature_content:
            other_json["DDB_Publisher"] = literature_content["Publisher"]
            comment_content += "DDB_Publisher: " + \
                literature_content["Publisher"] + "\n"
        if "MonographTitle" in literature_content:
            other_json["DDB_MonographTitle"] = literature_content["MonographTitle"]
            comment_content += "DDB_MonographTitle: " + \
                literature_content["MonographTitle"] + "\n"

        literature_item["cit_name"] = "DDB"
        literature_item["pub_type"] = "unknown"
        literature_item["net_src_state"] = False
        literature_item["cit_id"] = DDB_ID
        literature_item["extra_properties"] = other_json
        literature_item["comment"] = comment_content
        literature_item["status"] = status_entity
        literature_item["liter_type"] = liter_type_entity
        literature_item["creator"] = user_entity
        literature_item["last_editor"] = user_entity
        literature_item["reviewer"] = user_entity

        literature_entity = Literature.objects.create(**literature_item)
        if author1 is not None:
            literature_entity.authors.set([author1])
        literature_ID_literature_dict[DDB_ID] = literature_entity

    return literature_ID_literature_dict


def store_dataset(substance_DSI_substance_dict, literature_ID_literature_dict, literature_list_json):
    for literature_content in literature_list_json:
        literature_entity1 = literature_ID_literature_dict[literature_content["ID"]]
        DataTables_list = literature_content["DataTables"]
        for DataTables_content in DataTables_list:

            UTI = DataTables_content["UTI"]
            # 判断是否已经存在，存在则跳过
            if ExpDataset.objects.filter(dataset_code=UTI).first() is not None:
                continue

            name = DataTables_content["TableTitle_E"]
            zh_name = DataTables_content["TableTitle_D"]
            description = ""
            if "TableRemark_E" in DataTables_content:
                description += DataTables_content["TableRemark_E"] + "\n"
            if "TableRemark_D" in DataTables_content:
                description += DataTables_content["TableRemark_D"] + "\n"
            comment = ""
            if "DataCollection" in DataTables_content:
                comment += "DataCollection: " + \
                    DataTables_content["DataCollection"] + "\n"
            if "UTI" in DataTables_content:
                comment += "UTI: " + DataTables_content["UTI"] + "\n"
            expdataset_entity1 = ExpDataset.objects.create(
                name=name,
                zh_name=zh_name,
                description=description,
                comment=comment,
                source_liter=literature_entity1,
                purpose=purpose_entity,
                dataset_code=UTI,
                data_present_way=DataPresentWay_entity,
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity
            )
            # 处理组分
            component_list_1 = []
            for component_content in DataTables_content["ChemicalSystem"]["substance_list"]:
                DSI_ID = component_content["DSI"]
                component_entity1 = substance_DSI_substance_dict[DSI_ID]
                component_list_1.append(component_entity1)
            expdataset_entity1.components.set(component_list_1)
            # 处理物性和数据点
            # 将所有的都认为是物性
            property_id_property_dict = {}
            ExpDatasetPropertyMap_id_ExpDatasetPropertyMap_dict = {}
            for column_content in DataTables_content["ColumnDefinition"]:
                column_id = column_content["ID"]
                # 获取单位
                unit_entity1 = Unit.objects.filter(
                    symbol=column_content["Unit"]).first()
                if unit_entity1 is None:
                    unit_entity1 = Unit.objects.create(
                        unit_name=column_content["Unit"],
                        unit_name_en=column_content["Unit"],
                        symbol=column_content["Unit"],
                        is_system = True,
                        dimension=dimension_less_entity,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    UnitSymbol.objects.create(
                        symbol=column_content["Unit"],
                        is_system = True,
                        root_unit=unit_entity1,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    UnitConversion.objects.create(
                        base_unit=unit_entity,
                        target_unit=unit_entity1,
                        factor=1,
                        offset=0,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    # 绑定量和单位
                    QuantityUnitMap.objects.create(
                        quantity=quantity_entity,
                        unit=unit_entity1
                    )

                column_name = column_content["PropType"]
                if "PropSubType" in column_content:
                    column_name += " ; " + column_content["PropSubType"]
                property_entity1 = Property.objects.filter(
                    name=column_name).first()
                if property_entity1 is not None:
                    property_id_property_dict[column_id] = property_entity1
                    continue
                property_entity1 = Property.objects.create(
                    name=column_name,
                    name_en=column_name,
                    property_group=property_group_entity,
                    quantity=quantity_entity,
                    common_used=True,
                    is_single_value=True,
                    is_fixed_value=True,
                    is_one_variable=True,
                    is_two_variable=True,
                    is_mixz=True,
                    is_mixv=True,
                    is_reaction=True,
                    is_need_solvent_binary=True,
                    is_need_solvent_ternary=True,
                    description="ddb数据初始化",
                    create_person=user_entity,
                    modify_person=user_entity,
                )
                property_id_property_dict[column_id] = property_entity1
            # 构造数据集的物性
            for id in property_id_property_dict:
                ExpDatasetPropertyMap_entity1 = ExpDatasetPropertyMap.objects.create(
                    exp_dataset=expdataset_entity1,
                    property=property_id_property_dict[id],
                    unit=unit_entity1,
                )
                ExpDatasetPropertyMap_id_ExpDatasetPropertyMap_dict[id] = ExpDatasetPropertyMap_entity1
            # 处理数据点
            data_point_list = []
            for datapoint_content in DataTables_content["Data"]:
                data_point_1 = DataPoint.objects.create()

                for single_datapoint_content in datapoint_content:
                    # 分为情况
                    json_item = single_datapoint_content
                    final_value = -99999
                    if "Value" in single_datapoint_content:
                        if is_decimal_or_scientific(single_datapoint_content["Value"]):
                            final_value = float(
                                single_datapoint_content["Value"])
                        else:
                            final_value = -99999
                        json_item = single_datapoint_content
                    elif "LowerBound" in single_datapoint_content or "UpperBound" in single_datapoint_content:
                        final_value = -99998
                    elif "AlphaNumericValue" in single_datapoint_content:
                        final_value = -99997
                    else:
                        final_value = -99996
                    DataPointExpDatasetPropertyMap.objects.create(
                        data_point=data_point_1,
                        exp_dataset_property_map=ExpDatasetPropertyMap_id_ExpDatasetPropertyMap_dict[
                            single_datapoint_content["ID"]],
                        value=final_value,
                        extra_properties=json_item),
                data_point_list.append(data_point_1)
            expdataset_entity1.data_points.set(data_point_list)

    return None


def insert_single_ddb_file(file_path):
    # 先把组分本体新建了，
    processed_data_json = json.load(open(file_path, "r", encoding="utf-8"))
    substance_DSI_substance_dict = store_substance(
        processed_data_json["Substances"])
    literature_list_json = processed_data_json["Bibliographys"]
    literature_ID_literature_dict = store_literature(literature_list_json)
    store_dataset(substance_DSI_substance_dict,
                  literature_ID_literature_dict, literature_list_json)


def init_ddb_data_v2():
    # 路径配置
    # TODO: 需要修改为实际路径
    data_dir_path = "D:/Desktop/program/ppdbManagement/data-analysis/ddb_analysis_v2/processed_data/all_ddb_data3/"
    if not os.path.exists(data_dir_path):
        print("Data directory not found!", data_dir_path)
        return
    data_storate_path = os.path.join(data_dir_path, "storage_data")
    if not os.path.exists(data_storate_path):
        print("Data storage directory not found!", data_storate_path)
        return
    # 记录数据文件夹
    backend_file_path = "./files"
    if not os.path.exists(backend_file_path):
        os.mkdir(backend_file_path)
    ddb_dir_path = os.path.join(backend_file_path, "ddb_dataV2")
    if not os.path.exists(ddb_dir_path):
        os.mkdir(ddb_dir_path)
    # 获取data_dir_path最后一个文件夹的名称
    ddb_data_version = os.path.basename(data_dir_path)
    ddb_data_file_path = os.path.join(ddb_dir_path, ddb_data_version)
    if not os.path.exists(ddb_data_file_path):
        os.mkdir(ddb_data_file_path)
    ddb_log_file = os.path.join(ddb_data_file_path, "ddb_log.log")
    if os.path.exists(ddb_log_file):
        os.remove(ddb_log_file)

   # 获取文件夹里的所有文件
    files = os.listdir(data_storate_path)
    num = 0
    for file in tqdm(files, desc="Processing ddb data"):
        file_path = os.path.join(data_storate_path, file)
        try:
            num += 1
            # if num > 1:
            #     break
            # if num % 5 != 1 :
            #     continue
            # if num > 1000:
            #     break
            # print(file_path)
            insert_single_ddb_file(file_path)

        except Exception as e:
            # print(file_path)
            # traceback.print_exc()
            exc_str = traceback.format_exc()
            # return
            with open(ddb_log_file, "a") as f:
                error_message = "Error file: " + file_path + \
                    "\n" + exc_str + "\n" + "-"*50 + "\n"
                f.write(error_message)
